package com.example.springbootvuetest.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootvuetest.base.Result;
import lombok.RequiredArgsConstructor;
import com.example.springbootvuetest.entity.Favorite;
import com.example.springbootvuetest.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * <p>
 * controller
 * </p>
 *
 * @author shuoye
 * @since 2023-03-14
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - ")
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    private final IFavoriteService service;

    @PostMapping("/add")
    @ApiOperation(value = "加入收藏")
    public Result createOne(@RequestBody Favorite entity) {
        boolean flag = service.save(entity);
        return flag ? Result.success() : Result.error();
    }

    @GetMapping("/delete")
    @ApiOperation(value = "取消收藏")
    public Result delete(@RequestParam("id") Integer id) {
        boolean flag = service.removeById(id);
        return flag ? Result.success() : Result.error();
    }

    @GetMapping("/my")
    @ApiOperation(value = "我的收藏")
    public Result getUserFavorite(@RequestParam("userId") Integer userId) {
        List<Favorite> favorite = service.getAllFavorite(userId);
        return Result.success(favorite);
    }

    @GetMapping("/check")
    @ApiOperation(value = "检查是否为用户的购物车里面的东西")
    public Result<Integer> checkFavorite(@RequestParam("idleId") Integer idleId,
                                         @RequestParam("userId") Integer userId) {
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper<Favorite>();
        queryWrapper.eq("user_id", userId).eq("idle_id", idleId);
        Favorite one = service.getOne(queryWrapper);
        return one == null ? Result.success(0) : Result.success(one.getId());
    }
}